package com.example.wxback.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wxback.dao.ActivationCodeDao;
import com.example.wxback.model.ActivationCodePo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivationCodeService {

    @Autowired
    private ActivationCodeDao activationCodeDao;

    public ActivationCodePo selectById(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return activationCodeDao.selectOne(queryWrapper);
    }

    /**
     * 判断激活码是否已存在，防止重复
     *
     * @param code
     * @return
     */
    public boolean checkCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code", code);
        return activationCodeDao.selectCount(queryWrapper) > 0;
    }

    public boolean insert(ActivationCodePo activationCodePo) {
        return activationCodeDao.insert(activationCodePo) > 0;
    }

    public int countList(JSONObject jsonObject) {
        return activationCodeDao.countList(jsonObject);
    }

    public List<JSONObject> findList(JSONObject jsonObject) {
        return activationCodeDao.findList(jsonObject);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int deleteById(String id) {
        return activationCodeDao.deleteById(id);
    }

    /**
     * 查询激活码信息
     *
     * @param activateCode
     * @return
     */
    public ActivationCodePo selectByActivateCode(String activateCode) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code", activateCode);
        return activationCodeDao.selectOne(queryWrapper);
    }

    /**
     * 更新绑定的手机号
     *
     * @param activationCodePo
     * @return
     */
    public int bindActivateCodeByPhone(ActivationCodePo activationCodePo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code", activationCodePo.getCode());
        return activationCodeDao.update(activationCodePo, queryWrapper);
    }

    /**
     * 批量删除
     * @param codeList
     * @return
     */
    public int deleteMulityCodes(List<String> codeList) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", codeList);
        return activationCodeDao.delete(queryWrapper);
    }
}
